import 'package:flutter/material.dart';
import 'routes.dart';

class RouteObservers<R extends Route<dynamic>> extends RouteObserver<R> {
  @override
  void didPush(Route route, Route? previousRoute) {
    super.didPush(route, previousRoute);
    var name = route.settings.name ?? '';
    if (name.isNotEmpty) AppPages.history.add(name);
  }

  @override
  void didPop(Route route, Route? previousRoute) {
    super.didPop(route, previousRoute);
    var name = route.settings.name ?? '';
    if (name.isNotEmpty) AppPages.history.remove(name);
  }

  @override
  void didReplace({Route? newRoute, Route? oldRoute}) {
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
    if (newRoute != null) {
      var index = AppPages.history
          .indexWhere((element) => element == oldRoute?.settings.name);
      var name = newRoute.settings.name ?? '';
      if (name.isNotEmpty) {
        if (index > 0) {
          AppPages.history[index] = name;
        } else {
          AppPages.history.add(name);
        }
      }
    }
  }

  @override
  void didRemove(Route route, Route? previousRoute) {
    super.didRemove(route, previousRoute);
    AppPages.history.remove(route.settings.name);
  }

  @override
  void didStartUserGesture(Route route, Route? previousRoute) {
    super.didStartUserGesture(route, previousRoute);
  }

  @override
  void didStopUserGesture() {
    super.didStopUserGesture();
  }
}




// class RouteObservers<R extends Route<dynamic>> extends RouteObserver<R> {
//   @override
//   void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
//     super.didPush(route, previousRoute);
//     var name = route.settings.name ?? '';
//     if (name.isNotEmpty) AppPages.history.add(name);
//     print('didPush');
//     print(AppPages.history);
//   }

//   @override
//   void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
//     super.didPop(route, previousRoute);
//     AppPages.history.remove(route.settings.name);
//     print('didPop');
//     print(AppPages.history);
//   }

//   @override
//   void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
//     super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
//     if (newRoute != null) {
//       var index = AppPages.history.indexWhere((element) {
//         return element == oldRoute?.settings.name;
//       });
//       var name = newRoute.settings.name ?? '';
//       if (name.isNotEmpty) {
//         if (index > 0) {
//           AppPages.history[index] = name;
//         } else {
//           AppPages.history.add(name);
//         }
//       }
//     }
//     print('didReplace');
//     print(AppPages.history);
//   }

//   @override
//   void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
//     super.didRemove(route, previousRoute);
//     AppPages.history.remove(route.settings.name);
//     print('didRemove');
//     print(AppPages.history);
//   }

//   @override
//   void didStartUserGesture(
//       Route<dynamic> route, Route<dynamic>? previousRoute) {
//     super.didStartUserGesture(route, previousRoute);
//   }

//   @override
//   void didStopUserGesture() {
//     super.didStopUserGesture();
//   }
// }
